package learningThread.automic;

import java.util.concurrent.atomic.AtomicInteger;

public class AutomicUtils {

    private static  AtomicInteger atomicInteger = new AtomicInteger();

    /**
     * 设置一个共享值
     * @param value
     * @return
     */
    public static void setInitailVal(Integer value){
        atomicInteger.set(value);
        System.out.println("设置初始值为:"+atomicInteger.get());
    }

    public static int get(){
        return atomicInteger.get();
    }

    public static int incrementAndGet(){
        return atomicInteger.incrementAndGet();
    }

    public static boolean compareAndSet(Integer except,Integer value){
        if (atomicInteger.compareAndSet(except, value)) {
            System.out.println("线程"+Thread.currentThread().getName()+"修改成功");
            return true;
        }
        System.out.println("线程"+Thread.currentThread().getName()+"修改失败");
        return false;
    }
}
